﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace KstopaCommon
{
    public partial class PasswordForm : Form
    {
        UserType _username;

        string _oldpassword = "";

        string _newpassword1 = "";

        string _newpassword2 = "";

        public Dictionary<UserType, string> DictNamePassword;

        public PasswordForm(Dictionary<UserType, string> dict_name_password)
        {
            InitializeComponent();
            if (dict_name_password == null)
            {
                DictNamePassword = new Dictionary<UserType, string>();
            }
            else
            {
                DictNamePassword = dict_name_password;
            }
            string[] types = Enum.GetNames(typeof(UserType));
            foreach (var item in types)
            {
                if (item == UserType.None.ToString())
                {
                    //cmb_User.Items.Add(" ");
                    continue;
                }
                cmb_User.Items.Add(item);
            }
        }

        private void PasswordForm_Load(object sender, EventArgs e)
        {
            //cmb_User.SelectedIndex = (int)UserType.操作员;
            cmb_User.SelectedIndex = (int)UserType.操作员 - 1;
        }

        private void cmb_users_SelectedIndexChanged(object sender, EventArgs e)
        {
            //_username = (UserType)cmb_User.SelectedIndex;
            _username = (UserType)(cmb_User.SelectedIndex + 1);
        }

        private void tb_PasswordOld_TextChanged(object sender, EventArgs e)
        {
            try
            {
                _oldpassword = tb_PasswordOld.Text.Trim();
            }
            catch
            {
            }
        }

        private void tb_PasswordNew_TextChanged(object sender, EventArgs e)
        {
            try
            {
                _newpassword1 = tb_PasswordNew.Text.Trim();
            }
            catch
            {
            }
        }

        private void tb_PasswordConfirm_TextChanged(object sender, EventArgs e)
        {
            try
            {
                _newpassword2 = tb_PasswordConfirm.Text.Trim();
            }
            catch
            {
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_ConfirmOk_Click(object sender, EventArgs e)
        {
            if (_username == UserType.None || _username.ToString() == "")
            {
                MessageBox.Show("请选择用户","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (_oldpassword == "" || _newpassword1 == "" || _newpassword2 == "")
            {
                MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string password;
            DictNamePassword.TryGetValue(_username, out password);
            if (password != _oldpassword && password != "")
            {
                MessageBox.Show("请输入正确原始密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (_newpassword1 != _newpassword2)
            {
                MessageBox.Show("请重新输入确认密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            DictNamePassword[_username] = _newpassword1;
            this.DialogResult = DialogResult.OK;
        }

    }
}
